package com.park.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.Meta;
import com.park.model.Router;
import com.park.model.SysMenu;
import com.park.model.SysRelationKey;
import com.park.model.UserGetInfo;
import com.park.model.UserList;
import com.park.service.PermissionsService;
import com.park.service.SysMenuService;
import com.park.service.SysRelationKeyService;
import com.park.service.UserListService;
import com.park.service.Utility;

@Controller
@RequestMapping("/sysMenu")
public class SysMenuController {
	
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private SysRelationKeyService sysRelationKeyService;
	@Autowired
	private UserListService userListService;
	@Autowired
	private PermissionsService permissionsService;
	private static Log logger = LogFactory.getLog(SysMenuController.class);
	
	@RequestMapping(value = "/getByUserName",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getByUserName(@RequestBody Map<String,Object> args) {
		logger.info("[getByUserName]参数:"+args);
		Map<String,Object> result = new HashMap<String, Object>();
		String userName = (String)args.get("userName");
		UserList userList = userListService.getByUserName(userName);
		logger.info(userList);
		List<Router> rootMenu = sysMenuService.findTree(userName);
		List<String> roles = permissionsService.getByUserId(userList.getId());
		UserGetInfo userInfo = new UserGetInfo();
		userInfo.setRouters(rootMenu);
		userInfo.setAvatar(userList.getAvatar());
		userInfo.setUser_id(userList.getId());
		userInfo.setUsername(userList.getUserListname());
		userInfo.setRoles(roles);
		result.put("status", 1001);
		result.put("body", userInfo);
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value = "/insert",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String insert(@RequestBody SysMenu sysMenu) {
		logger.info("[insert]参数:"+sysMenu);
		Map<String,Object> result = new HashMap<String, Object>();
//		try {
			int count = sysMenuService.insert(sysMenu);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "添加成功");
		}else {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
//		}catch (Exception e) {
//			result.put("status", 1002);
//			result.put("message", e.getMessage());
//			result.put("message", "添加失败");
//		}
		logger.info("[insert]返回值:"+result);
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/update",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String update(@RequestBody SysMenu sysMenu) {
		logger.info("[update]参数:"+sysMenu);
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			int count = sysMenuService.update(sysMenu);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "修改成功");
		}else {
			result.put("status", 1002);
			result.put("message", "修改失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "修改失败");
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/delete",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String delete(@RequestBody Map<String,Object> args) {
		logger.info("[delete]参数:"+args);
		int id = (int)args.get("id");
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			int count = sysMenuService.delete(id);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "删除成功");
		}else {
			result.put("status", 1002);
			result.put("message", "删除失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "删除失败");
		}
		return Utility.gson.toJson(result);
	}
	
	@Transactional
	@RequestMapping(value = "/handleSysRelationKey",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String handleSysRelationKey(@RequestBody List<SysRelationKey> sysRelationKeys) {
		logger.info("[handleSysRelationKey]参数:"+sysRelationKeys);
		int roleId = sysRelationKeys.get(0).getPermissionsId();
		int size = sysRelationKeys.size();
		int deleteCount = sysRelationKeyService.deleteByRoleId(roleId);
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			int count = sysRelationKeyService.insert(sysRelationKeys);
		if(count == size) {
			result.put("status", 1001);
			result.put("message", "添加成功");
		}else {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		logger.info("[handleSysRelationKey]返回值:"+result);
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/updateSysRelationKey",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String updateSysRelationKey(@RequestBody SysRelationKey sysRelationKey) {
		logger.info("[updateSysRelationKey]参数:"+sysRelationKey);
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			int count = sysRelationKeyService.update(sysRelationKey);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "添加成功");
		}else {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value = "/deleteSysRelationKey",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String deleteSysRelationKey(@RequestBody int id) {
		logger.info("[deleteSysRelationKey]参数:"+id);
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			int count = sysRelationKeyService.delete(id);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "添加成功");
		}else {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/getMenu",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getMenu(@RequestBody Map<String,Object> args) {
		logger.info("[getMenu]参数:"+args);
		Map<String,Object> result = new HashMap<String, Object>();
		String userName = (String)args.get("userName");
		UserList userList = userListService.getByUserName(userName);
		logger.info(userList);
		List<Router> rootMenu = sysMenuService.findTree(userName);
		logger.info(rootMenu);
		result.put("status", 1001);
		result.put("body", rootMenu);
		return Utility.gson.toJson(result);
	}
}
